#pragma once

#include <list>
using std::list;

#include "config.h"

#ifdef DEBUG_OPEN_CONSOLE

#include <windows.h>
#include <fcntl.h>
#include <io.h>

void SAFE_OPEN_CONSOLE_INIT();
void SAFE_OPEN_CONSOLE_FINALIZE();
void SAFE_SETCONSOLETITLE_FINALIZE(char* title);

#endif //DEBUG_OPEN_CONSOLE

#ifdef DEBUG_ASSERT

#define ASSERT(x)  if(!(x)) { __asm int 3 }

#else

#define ASSERT(x)  

#endif

#ifdef DEBUG_NEW_DELETE

#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
//_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

typedef struct _LOG_MSG_INFO
{ 
	static const int FILE_NAME_SIZE = 128;

	_LOG_MSG_INFO()
		: ptr(NULL)
		, size(0)
		, line(0)
	{
		memset(file, 0, FILE_NAME_SIZE);
	}

	void*			ptr;
	uint16			size; 
	char			file[FILE_NAME_SIZE]; 
	int				line;
}LogMsgInfo;

typedef list<LogMsgInfo>		LogMsgInfoList;

void AppendLogMsg(void* ptr, size_t size, const char* file, const int line);
void RemoveLogMsg(void* ptr);

#define SAFE_NEW   new(__FILE__, __LINE__)

void* operator new(size_t size, const char* file, const int line);
void operator delete(void* ptr, const char* file, const int line);
void operator delete(void* ptr);

#else

#define SAFE_NEW   new

#endif //DEBUG_NEW_DELETE

#define SAFE_DELETE_ARRAY(a) { if((a) != NULL) { delete[] (a); (a) = NULL; } }
#define SAFE_DELETE(a)		 { if((a) != NULL) { delete   (a); (a) = NULL; } }